package org.naxx.security.xgen.parts;

import java.util.Date;

import org.naxx.security.model.Condition;
import org.naxx.security.xgen.XExpressionGenerator;
import org.naxx.security.xgen.util.XWriter;

public final class XConditionGenerator extends XAbstractGenerator {
	
	public void generateConditionClass(XWriter writer, Condition condition, 
			String className) throws Exception {
		
		this.writeHeader(writer, className);
		
		writer.write("protected Boolean exec() {");
		XExpressionGenerator x = new XExpressionGenerator();
		String rootVariableName = x.generate(writer, condition.getExpressionTree().getRoot());
		writer.write("return " + rootVariableName + ";");
		writer.write("}");
		writer.write("");
		
		this.writeFooter(writer);
	}
	
	void writeHeader(XWriter writer, String className) throws Exception {
		writer.write("// Auto generated on " + new Date());
		writer.write("package org.naxx.security.generated;");
		writer.write("");
		writer.write("import org.naxx.security.naxxlet.model.XAbstractCondition;");
		writer.write("");
		writer.write("public final class " + className + " extends "
					+ " XAbstractCondition {");
		writer.write("");
	}
	
	void writeFooter(XWriter writer) throws Exception {
		writer.write("");
		writer.write("}" + " // end of generated class");
	}
}
